#include <linux/kernel.h>

#include <linux/module.h>

#include <linux/netfilter.h>

#include <linux/netfilter_ipv4.h>

#include <linux/ip.h>

#include <linux/in.h>

#include <net/ip.h>

#include <linux/tcp.h>


#define SRC 0
#define DST 1

static struct nf_hook_ops netfilter_ops_in;
static struct nf_hook_ops nf_prerouting;

//typedef unsigned short u16;
//typedef unsigned long u32;

unsigned short tmp_port;




unsigned int inet_addr(char *str) 
{ 
	int a,b,c,d; 
        char arr[4]; 
        sscanf(str,"%d.%d.%d.%d",&a,&b,&c,&d); 
        arr[0] = a; arr[1] = b; arr[2] = c; arr[3] = d; 
        return *(unsigned int*)arr; 
} 

void inet_addr_r(int arr)
{
	int a,b,c,d;
	a=(arr & 0xff000000)>>24;
	b=(arr & 0x00ff0000)>>16;
	c=(arr & 0x0000ff00)>>8;
	d=(arr & 0x000000ff);
	printk("%d.%d.%d.%d ",d,c,b,a);
}

unsigned short getIPHeaderLength(struct iphdr *iph)
{
	unsigned int *tmp=iph;
	return ((*tmp)&0x0F)*2;
}

unsigned short getTCPport(int src_dst,struct iphdr* iph)
{
	unsigned short *u16=iph;

	struct tcphdr* tcph= u16+getIPHeaderLength(iph);

	if (src_dst==SRC)
	{
		return tcph->source;
	}
	else if (src_dst==DST)
	{
		return tcph->dest;
	}
}


void modifyHeader(struct iphdr *iph,struct sk_buff *skb,int src_dst, char* ipaddr, unsigned short port)
{

	if (src_dst==SRC)
	//Alter the source address 192.168.10.108
		iph->saddr=inet_addr(ipaddr);

	else if (src_dst=DST)
		iph->daddr=inet_addr(ipaddr);
	
	//Alter the checksum
	unsigned short *u16;
	int i=0;
	unsigned long sum=0;
	for (u16=iph;i<10;u16++,i++)
	{
		if (i!=5)	//checksum=0
			sum+=*u16;
	}

	// take only 16 bits out of the 32 bit sum and add up the carries
	while (sum>>16)
	  sum = (sum & 0xFFFF)+(sum >> 16);

	// one's complement the result
	sum = ~sum;

	iph->check=(unsigned short)sum;
	//printk("sum %hu\n",(unsigned short)sum);


	//Alter the # of port
	//printk("source port: %hx\n",*u16);
	//*u16=0x439b;

	//printk("tcp start at: %hx\n",u16);
	//printk("tail: %hx\n",skb->tail);

	unsigned short* tcp_header_ptr =u16;

	//Alter the port number
	unsigned short* port_pt;
	if (src_dst==SRC)
	{
		port_pt=tcp_header_ptr;
		tmp_port=*port_pt;
		
	}

	else if (src_dst==DST)
	{
		port_pt=tcp_header_ptr+1;
	}

	*port_pt=port;
	

	/*
	for(i=0;i<50 && u16<skb->tail;u16++,i++)
	{
		printk("%hx ",*u16);
	}
	printk("\ni value: %d\n",i);
	*/



	/*
	unsigned short* s_pt=iph->saddr;
	unsigned short* d_pt=iph->daddr;
	unsigned short prot_tcp=6;		//# of TCP protocol is 6
	
	sum=0;
	int padd=0;
	unsigned short len_tcp=0;
	unsigned short word=0;


	//Get the length of tcp header & content
	len_tcp=(unsigned short) skb->tail - (unsigned short) u16;

	//If len_tcp is odd, add a padding byte = 0 at the end of packet
	if (len_tcp%2==1)
		padd=1;

	for(i=0;i<len_tcp+padd;i++)
	{
		if (i==len_tcp)
		{
			//word=(((tcp_header_ptr+i)<<8)&0xFF00);
			sum+=*(tcp_header_ptr+i)&0xFF00;
		}
		else
		{
			//word=(((tcp_header_ptr+i)<<8)&0xFF00)+((tcp_header_ptr+i+1)&0x00FF);
			sum+=*(tcp_header_ptr+i);
		}
		//sum = sum + (unsigned long)word;
	}

	

	// add the TCP pseudo header which contains:
	// the IP source and destinationn addresses,
	
	sum+=*s_pt;
	s_pt++;
	sum+=*s_pt;
	sum+=*d_pt;
	d_pt++;
	sum+=*d_pt;
	
	//add protocol number and length of TCP packet
	sum+=prot_tcp+len_tcp;

	// keep only the last 16 bits of the 32 bit calculated sum and add the carries
    	while (sum>>16)
		sum = (sum & 0xFFFF)+(sum >> 16);
		
	// Take the one's complement of sum
	sum = ~sum;
	
	printk("TCP checksum: %hx\n",sum);

	*/


	//modify the checksum of tcp header

	struct tcphdr* tcph=tcp_header_ptr;

       tcph->check = 0;
       int tot_len = ntohs(iph->tot_len);
       int iph_len = ip_hdrlen(skb);
       skb->csum = csum_partial((unsigned char *)tcph, tot_len - iph_len,0);
       tcph->check = csum_tcpudp_magic(iph->saddr,
                                       iph->daddr,
                                       tot_len - iph_len,iph->protocol,
                                       skb->csum);
	
	

}


unsigned int main_hook(unsigned int hooknum,

	struct sk_buff *skb,

	const struct net_device *in,

	const struct net_device *out,

	int (*okfn)(struct sk_buff*))

{

	struct iphdr *iph;



	if (!skb) return NF_ACCEPT;



	iph = ip_hdr(skb);

	if (!iph) return NF_ACCEPT;

	
	/*	For netfilter_ops_in.hooknum = NF_INET_PRE_ROUTING:
	*	If the ip packet comes from localhost, the iph->saddr is the target address and iph->daddr is the local ip
		But if packet comes from other machines in LAN, the iph->saddr is the LAN machine address and iph->daddr is the target address
	*
	*/

	
	if (iph->protocol==IPPROTO_TCP)
	{
		if (iph->saddr==inet_addr("10.0.0.2"))
			modifyHeader(iph,skb,SRC,"192.168.10.108",0x439b);

	}


	return NF_ACCEPT;

}

unsigned int main_hook_prerouting(unsigned int hooknum,

	struct sk_buff *skb,

	const struct net_device *in,

	const struct net_device *out,

	int (*okfn)(struct sk_buff*))

{

	struct iphdr *iph;



	if (!skb) return NF_ACCEPT;



	iph = ip_hdr(skb);

	if (!iph) return NF_ACCEPT;

	
	if (iph->protocol==IPPROTO_TCP)
	{
		if (iph->daddr==inet_addr("192.168.10.108") && getTCPport(DST,iph)==0x439b )
		{
			modifyHeader(iph,skb,DST,"10.0.0.2",tmp_port);
		}
	}
	

	/*
	printk("Protocol: %d ",iph->protocol);
	printk("Source: ");
	inet_addr_r(iph->saddr);
	printk("Destnation: ");
	inet_addr_r(iph->daddr);
	printk("\n");
	*/

	return NF_ACCEPT;

}



static int __init init(void)

{

	printk("*************************\n");
	netfilter_ops_in.hook = main_hook;

	netfilter_ops_in.pf = PF_INET;

	netfilter_ops_in.hooknum = NF_INET_POST_ROUTING;

	netfilter_ops_in.priority = NF_IP_PRI_FIRST;

	nf_register_hook(&netfilter_ops_in);


	nf_prerouting.hook=main_hook_prerouting;
	nf_prerouting.pf=PF_INET;
	nf_prerouting.hooknum=NF_INET_PRE_ROUTING;
	nf_prerouting.priority=NF_IP_PRI_FIRST;
	nf_register_hook(&nf_prerouting);


	return 0;

}



static void __exit cleanup(void)

{

	nf_unregister_hook(&netfilter_ops_in);
	nf_unregister_hook(&nf_prerouting);

}



module_init(init);

module_exit(cleanup);



MODULE_LICENSE("GPL");
